import { Component, Input, OnInit } from '@angular/core';
import { AppService } from 'src/services/app.service';
import { DataService } from 'src/services/data.service';
import { StoreService } from 'src/services/store.service';
import { UiService } from 'src/services/ui.service';

@Component({
  selector: 'app-propertyview',
  templateUrl: './propertyview.component.html',
  styleUrls: ['./propertyview.component.scss']
})
export class PropertyviewComponent implements OnInit {

  constructor(private storeService: StoreService,
    private dataService: DataService,
    private ui: UiService,
    private appService: AppService) { }

  ngOnInit(): void {
  }
  private sort = 0;
  confirm: boolean = false;
  setparent: boolean = false;
  public get getEntity() {
    if (this.entity === undefined) {
      return [];
    }
    if (this.sort === -1) {
      return Object.keys(this.entity).sort((x, y) => {
        return x[0] > y[0] ? 1 : x[0] === y[0] ? 0 : -1;
      });
    } else if (this.sort === 1) {
      return Object.keys(this.entity).sort((x, y) => {
        return x[0] < y[0] ? 1 : x[0] === y[0] ? 0 : -1;
      });
    } else {
      return Object.keys(this.entity);
    }
  }

  @Input()
  public entity: any;


  readOnly(item: string) {
    if (
      item === 'Id' ||
      item === 'id' ||
      item === 'ID' ||
      item === 'userId' ||
      item === 'scopeId'
    )
      return true;
    return false;
  }
  public sortProperty(): void {
    switch (this.sort) {
      case -1:
        this.sort = 0;
        break;
      case 0:
        this.sort = 1;
        break;
      default:
        this.sort = -1;
        break;
    }
  }
  public confirmed() {
    this.confirm = false;
    if (this.options === 'save')
      this.dataService.saveEntity(this.storeService.currentEntity);
    if (this.options === 'remove')
      this.dataService.removeEntity(this.storeService.currentEntity);
  }
  /**
   * saveClick()
   */
  public saveClick() {
    // if (!this.appService.validPermission()) {
    //   return;
    // }
    this.options = 'save';
    this.confirm = true;
  }
  options = '';
  public removeClick() {
    // if (!this.appService.validPermission()) {
    //   return;
    // }
    this.options = 'remove';
    this.confirm = true;
  }

  confirmset() {
    // this.setparent = false;
    // if (this.storeService.selectEntityType == 'department') {
    //   console.log('parent');
    //   console.log(this.storeService.selectEntity);
    //   this.entity['parentId'] = (this.storeService
    //     .selectEntity as Department).id;
    // }
    // if (this.storeService.selectEntityType == 'role')
    //   this.entity['parentId'] = (this.storeService.selectEntity as Role).id;
    // if (this.storeService.selectEntityType == 'module')
    //   this.entity['parentId'] = (this.storeService.selectEntity as Module).id;
    // if (this.storeService.selectEntityType == 'group')
    //   this.entity['parentId'] = (this.storeService.selectEntity as Group).id;
  }
  setParent() {
    // this.storeService.selectEntityType = this.storeService.currentEntityType;
    // console.log(this.storeService.selectEntityType);
    // if (this.storeService.selectEntityType === 'department')
    //   this.storeService.selectDepartments = this.storeService.AllDepartments;

    // this.us.initSelectData();
    // this.setparent = true;
  }
}
